package BehavioralPatterns.Observer;

import java.util.ArrayList;
import java.util.List;

public class WeatherData implements Subject {

  private float temperature;
  private float pressure;
  private float humidity;
  private List<Observer> observers;

  public WeatherData() {
    this.observers = new ArrayList<Observer>();
  }

  public float getTemperature() {
    return temperature;
  }

  public float getPressure() {
    return pressure;
  }

  public float getHumidity() {
    return humidity;
  }

  public void dataChange(){
    notifyObserver();
  }

  public void setData(float temperature, float pressure, float humidity){
    this.temperature = temperature;
    this.pressure = pressure;
    this.humidity = humidity;
    dataChange();
  }

  @Override
  public void registerObserver(Observer o) {
    observers.add(o);
  }

  @Override
  public void remove(Observer o) {
    if (observers.contains(o)){
      observers.remove(o);
    }
  }

  @Override
  public void notifyObserver() {
    for (int i = 0; i < observers.size(); i++) {
      observers.get(i).update(this.temperature,this.pressure,this.humidity);
    }
  }
}
